package com.wwtech.epp.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.ninjia.ninjiabaselibrary.utils.SpUtil;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.db.CommentContentDao;
import com.wwtech.epp.db.DBManager;
import com.wwtech.epp.ui.activity.login.LoginActivity;
import com.wwtech.epp.ui.activity.mine.component.DaggerSettingComponent;
import com.wwtech.epp.ui.activity.mine.contract.SettingContract;
import com.wwtech.epp.ui.activity.mine.module.SettingModule;
import com.wwtech.epp.ui.activity.mine.presenter.SettingPresenter;
import com.wwtech.epp.utils.ToastUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.wwtech.epp.R.id.cancal;
import static com.wwtech.epp.R.id.sure;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.mine
 * @Description: $description
 * @date 2017/08/28 20:21:27
 */

public class SettingActivity extends BaseActivity implements SettingContract.View {

    @Inject
    SettingPresenter mPresenter;
    @BindView(R.id.account)
    TextView account;
    @BindView(R.id.new_message)
    TextView newMessage;
    @BindView(R.id.cache)
    TextView cache;
    @BindView(R.id.login_out)
    TextView loginOut;
    private DBManager mDbManager;
    private CommentContentDao mCommentContentDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("设置");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setupActivityComponent() {
        DaggerSettingComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .settingModule(new SettingModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(SettingContract.SettingContractPresenter presenter) {
        mPresenter = (SettingPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }


    private void loginOut() {
        SpUtil.putString(this, ContainValue.LOGINPHONE, "");
        SpUtil.putString(this, ContainValue.LOGINPASSWORD, "");
        mDbManager = DBManager.init(this);
        mCommentContentDao = mDbManager.getDaoSession().getCommentContentDao();
        mCommentContentDao.deleteAll();
        startActivity(new Intent(this, LoginActivity.class));
    }

    @OnClick({R.id.account, R.id.new_message, R.id.cache, R.id.login_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.account:
                startActivity(ReSetPasswordActivity.class);
                break;
            case R.id.new_message:
                startActivity(NewMessageInformActivity.class);
                break;
            case R.id.cache:
                AlertDialog alertDialog1 = new AlertDialog.Builder(this).create();
                View view2 = View.inflate(this, R.layout.two_choose_dialog, null);
                alertDialog1.setView(view2);
                alertDialog1.setCancelable(true);
                TextView tv_title1 = (TextView) view2.findViewById(R.id.content);
                tv_title1.setText("清除缓存？");
                TextView cancal1 = (TextView) view2.findViewById(cancal);
                TextView sure1 = (TextView) view2.findViewById(sure);
                cancal1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog1.dismiss();
                    }
                });
                sure1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog1.dismiss();
                        clearCache();
                    }
                });
                alertDialog1.show();
                break;
            case R.id.login_out:
                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                View view1 = View.inflate(this, R.layout.two_choose_dialog, null);
                alertDialog.setView(view1);
                alertDialog.setCancelable(true);
                TextView tv_title = (TextView) view1.findViewById(R.id.content);
                tv_title.setText("确定退出登录吗？");
                TextView cancal = (TextView) view1.findViewById(R.id.cancal);
                TextView sure = (TextView) view1.findViewById(R.id.sure);
                cancal.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });
                sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                        loginOut();
                    }
                });
                alertDialog.show();
                break;
        }
    }

    private void clearCache() {
        ToastUtil.showShort("清除缓存成功！");
    }
}